/**
 * 
 */
package com.palmwindActivity.app.main;

import java.util.Vector;

import com.palmwindActivity.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

/**
 * @author albertma
 * 
 */
public class LocalThumbnailAdapter extends BaseAdapter
{

    private Context context;

    private View.OnClickListener onClicklistener;

	private Vector<Bitmap> imageVector;

	private Drawable frameworkDrawable;
	
    private static int imageIds[] =
    { R.drawable.s01, R.drawable.s02, R.drawable.s03, R.drawable.s04, R.drawable.s05, };

    public LocalThumbnailAdapter(Context context)
    {
        this.context = context;
        Bitmap drawableBmp = BitmapFactory.decodeResource(
				context.getResources(), R.drawable.framework);
        frameworkDrawable = new BitmapDrawable(drawableBmp);
    }

	public int getCount() {
		if(this.imageVector != null && this.imageVector.size() > 0)
		{
			return (imageIds.length + this.imageVector.size());
		}
		return imageIds.length;

	}
	
	

    public Object getItem(int position)
    {
        return position;
    }

    public long getItemId(int position)
    {
        // TODO Auto-generated method stub
        return position;
    }

    public void setOnClickListener(View.OnClickListener onClicklistener)
    {
        this.onClicklistener = onClicklistener;
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
		ImageView imageView = null;
		if (position >= 0 && position < this.getCount()) {

			if (convertView == null) {
				imageView = new ImageView(context);
				imageView.setLayoutParams(new GridView.LayoutParams(120, 102));
				imageView.setAdjustViewBounds(false);
				imageView.setScaleType(ImageView.ScaleType.CENTER);
			
				imageView.setPadding(0, 0, 0, 0);
			} else {
				imageView = (ImageView) convertView;
			}
			imageView.setId(position);
			Bitmap bitmap = null;
			if(position < imageIds.length)
			{
				bitmap = BitmapFactory.decodeResource(
					context.getResources(), imageIds[position]);
			}
			else
			{
				bitmap = imageVector.get(position - imageIds.length);
			}
			int width = bitmap.getWidth();
	        int height = bitmap.getHeight();
	        int newWidth = 96;
	        int newHeight = 72;
	       
	        // calculate the scale - in this case = 0.4f
	        float scaleWidth = ((float) newWidth) / width;
	        float scaleHeight = ((float) newHeight) / height;
//	       
//	        // createa matrix for the manipulation
	        Matrix matrix = new Matrix();
	        // resize the bit map
	        matrix.postScale(scaleWidth, scaleHeight);
	        // rotate the Bitmap
//	        matrix.postRotate(45);
	 
	        // recreate the new Bitmap
	        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
	                          width, height, matrix, true);
	   
	        // make a Drawable from Bitmap to allow to set the BitMap
	        // to the ImageView, ImageButton or what ever

			if (resizedBitmap != null) {
				imageView.setImageBitmap(resizedBitmap);
			}
			imageView.setBackgroundDrawable(frameworkDrawable);
			imageView.setOnClickListener(onClicklistener);
		}
		return imageView;
    }

	public void setNetWorkData(Vector<Bitmap> imageVector) {
		// TODO Auto-generated method stub
		this.imageVector = imageVector;
		
	}

	public Vector<Bitmap> getNetBitmaps()
	{
		return this.imageVector;
	}

}
